package com.guoshengdeu.door.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoshengdeu.door.entity.Video;
import com.guoshengdeu.door.service.IVideoService;
import com.guoshengdeu.door.util.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author 861053543
 * @since 2021-7-2
 */
@Api("视频管理")
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private IVideoService videoService;

    //查询video
    @ApiOperation("查询")
    @RequestMapping("list")
    public List<Video> queryVideo(){
        return videoService.list();
    }

    //添加更新
    @ApiOperation("添加更新")
    @RequestMapping("addvideo")
    public RespResult addVideo(@RequestBody Video video){
        boolean saveOrUpdate = videoService.saveOrUpdate(video);
        if (saveOrUpdate){
            return RespResult.success();
        }
        return RespResult.error("添加或更新失败");
    }

    //删除video
    @ApiOperation("删除")
    @DeleteMapping("delevideo")
    public RespResult deleVideo(int id){
        boolean removeById = videoService.removeById(id);
        if (removeById){
            return RespResult.success();
        }
        return RespResult.error("删除失败");
    }

    //分页
    @ApiOperation("分页")
    @GetMapping("page")
    public RespResult Page(Page<Video> page){
        Page<Video> pages = videoService.page(page);
        return RespResult.success(pages);
    }

    @ApiOperation("文件上传")
    @RequestMapping("save")
    public RespResult upload(@RequestParam("upload") MultipartFile upload, HttpServletRequest request) {
        System.out.println("上传中...............");
        //1.获取文件上传的  原名称  使用 SpringMVC中  MultipartFile 实现文件上传
        String originalFilename = upload.getOriginalFilename();
        //2.准备文件上传的位置   服务器当前工程的某个位置
        String realPath = request.getSession().getServletContext().getRealPath("upload");
        //3.判断上传的位置  是否存在 ，   不存在创建
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdir();
//            upload.getBytes()
        }
        //4.拼接路径   将文件位置和源文件名称 连接 +  系统当前时间毫秒值
        String[] split = originalFilename.split("[.]");
        String name = UUID.randomUUID().toString().replace("-", "");
//        String fileName = realPath + File.separator + System.currentTimeMillis() + originalFilename;
        String filePath = name + "." + split[split.length-1];
        String fileName = realPath + File.separator + filePath;

        System.out.println("输出拼接地址== " + fileName);
        //5.实现文件上传
        try {
            upload.transferTo(new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            return RespResult.error("文件上传失败");

        }
        return RespResult.success(filePath);
    }
}
